package com.kingbook.core.generator;

import com.kingbook.common.utils.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Iterator;
import java.util.List;

/**
 * @Author: Bucky
 * @Date: 2019/1/2
 * @Version 1.0
 */
public class RestClassGenerator extends Generator {

    private static final Logger logger = LoggerFactory.getLogger(RestClassGenerator.class);

    private String target;

    private String packages;

    private String servicePackage;

    private List<TableBean> tables;

    private AnnotationInfo info;

    RestClassGenerator(String target, String packages, String servicePackage, List<TableBean> tables, AnnotationInfo info, boolean overWrite) {
        this.target = target + "/" + packages.replace(".", "/");
        this.packages = packages;
        this.servicePackage = servicePackage;
        this.tables = tables;
        this.info = info;
        setOverWrite(overWrite);
    }

    void gen() {
        String template = readTemplate("classRest.tpl");
        String javaBeanClass;
        StringBuilder notNullCheck;
        String pkCondition;
        String importService;
        String pkCheck;
        String pkPath;
        String pkVars;
        String pks;
        String tTemplate;
        for (TableBean bean : tables) {
            if (bean.isCreateService() && bean.isCreateRest()) {
                pkCondition = "";
                pkVars = "";
                pks = "";
                pkCheck = "";
                pkPath = "";
                notNullCheck = new StringBuilder();
                javaBeanClass = bean.getObjectName();
                importService = "\nimport " + servicePackage + "." + javaBeanClass + "Service;";
                for (String pk : bean.getPrimaryKeys().keySet()) {
                    String cpk = StringUtils.toCamelCase(pk);
                    pkPath += "{" + cpk + "}/";
                    pks += cpk + ",";
                    pkCheck += "\"" + cpk + "\",";
                    pkVars += "@PathVariable " + bean.getColumns().get(pk).javaType.getShortName() + " " + cpk + ",";
                }
                if (StringUtils.isNotEmpty(pks)) {
                    pks = pks.substring(0, pks.length() - 1);
                    pkPath = pkPath.substring(0, pkPath.length() - 1);
                    pkVars = pkVars.substring(0, pkVars.length() - 1);
                    pkCheck = pkCheck.substring(0, pkCheck.length() - 1);
                    pkCondition = "\n    @DeleteMapping(\"${pkPath}\")\n" +
                            "    public JSONResult remove(${pkVars}) {\n" +
                            "        return ${javaBeanName}Service.remove(${pks});\n" +
                            "    }\n" +
                            "\n" +
                            "    @PostMapping\n" +
                            "    @ContainProperties({${pkCheck}})\n" +
                            "    public JSONResult modify(@RequestBody Map<String, Object> param) {\n" +
                            "        return ${javaBeanName}Service.modify(param);\n" +
                            "    }\n" +
                            "\n" +
                            "    @GetMapping(\"${pkPath}\")\n" +
                            "    public JSONResult find(${pkVars}) {\n" +
                            "        return ${javaBeanName}Service.find(${pks});\n" +
                            "    }";
                }
                Iterator<ColumnBean> iterator = bean.getColumns().values().iterator();
                ColumnBean c;
                boolean hasNotNull = false;
                while (iterator.hasNext()) {
                    c = iterator.next();
                    if (!c.nullable && !bean.isPK(c)) {
                        hasNotNull = true;
                        notNullCheck.append("\"").append(StringUtils.toCamelCase(c.column)).append("\",");
                    }
                }
                if (hasNotNull) {
                    notNullCheck = new StringBuilder(notNullCheck.substring(0, notNullCheck.length() - 1));
                    notNullCheck.insert(0, "\n    @ContainProperties({").append("})");
                }
                tTemplate = template;
                writeTemplate(target + "/" + javaBeanClass + "Rest.java",
                        tTemplate.replace("${author}", info.getAuthor())
                                .replace("${time}", info.getTime())
                                .replace("${version}", info.getVersion())
                                .replace("${packages}", packages)
                                .replace("${notNullCheck}", notNullCheck)
                                .replace("${pkCondition}", pkCondition)
                                .replace("${importService}", importService)
                                .replace("${pkVars}", pkVars)
                                .replace("${pks}", pks)
                                .replace("${pkPath}", pkPath)
                                .replace("${pkCheck}", pkCheck)
                                .replace("${javaBeanClass}", javaBeanClass)
                                .replace("${javaBeanName}", StringUtils.toCapitalizeCamelLwCase(javaBeanClass)));
            }

        }

    }

}
